package com.example.CloudWeather

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.example.CloudWeather.storage.TokenPreferences

class MainActivity : AppCompatActivity() {
    private var viewPager: ViewPager2? = null

    // 2. 获取 ViewPager2 的适配器
    private var adapter: ViewPagerAdapter? = null
    // 3. 记录当前的索引值
    private var currentPosition = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.ac_main)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }
        // 1. 添加点击事件
        addClickEvent()
        // 2. 加载Fragment
        loadFragment()
        // 输出一下 保存到 TokenPreferences 里面的token
        val token = TokenPreferences.getInstance(this@MainActivity).getToken()
        Toast.makeText(this, token, Toast.LENGTH_SHORT).show()
    }

    /**
     * 加载Fragment
     */
    fun loadFragment(){
        // 1. 滑动控件处理
        if(viewPager == null){
            viewPager = findViewById(R.id.vpMain) // 初始化

            // 禁止页面滑动
            viewPager?.isUserInputEnabled = false

            // 注册页面切换监听
            viewPager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)

                    setCurrentTab(position)
                    currentPosition =  position
                }
            })
        }

        // 2. 初始化适配器
        if(adapter == null){
            adapter = ViewPagerAdapter(this)
        }

        viewPager?.adapter = adapter
        viewPager?.offscreenPageLimit = 2 // 缓存3个页面(0, 1, 2)
    }

    /**
     * 添加点击事件
     */
    fun addClickEvent(){
        // 1. 获取相关控件
        val bottomNavigationView = findViewById<ViewGroup>(R.id.llBottomNavigation)
        val homeContainer = bottomNavigationView.findViewById<ViewGroup>(R.id.nav_home)
        val lifeContainer = bottomNavigationView.findViewById<ViewGroup>(R.id.nav_life)
        val profileContainer = bottomNavigationView.findViewById<ViewGroup>(R.id.nav_profile)

        // 2. 首页点击事件
        homeContainer.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                // 实现页面跳转(通过viewPager)
                // 更新底部导航栏选中项
                setCurrentTab(0)
                viewPager?.currentItem = 0
                currentPosition = 0
            }
        })

        // 3. 收藏点击事件
        lifeContainer.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                // 实现页面跳转(通过viewPager)
                // 更新底部导航栏选中项
                setCurrentTab(1)
                viewPager?.currentItem = 1
                currentPosition = 1
            }
        })

        // 4. 我的点击事件
        profileContainer.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                // 实现页面跳转(通过viewPager)
                // 更新底部导航栏选中项
                setCurrentTab(2)
                viewPager?.currentItem = 2
                currentPosition = 2
            }
        })
    }

    /**
     * 设置底部Tab选中项
     */
    fun setCurrentTab(position: Int) {
        val bottomNavigationView = findViewById<ViewGroup>(R.id.llBottomNavigation)

        val tvHome = bottomNavigationView.findViewById<TextView>(R.id.tv_home)
        var imageHome = bottomNavigationView.findViewById<ImageView>(R.id.img_home)

        val tvLife = bottomNavigationView.findViewById<TextView>(R.id.tv_life)
        var imageLife = bottomNavigationView.findViewById<ImageView>(R.id.img_life)

        val tvProfile = bottomNavigationView.findViewById<TextView>(R.id.tv_profile)
        var imageProfile = bottomNavigationView.findViewById<ImageView>(R.id.img_profile)

        when (position) {
            0 -> {
                tvHome.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageHome.setColorFilter(resources.getColor(R.color.weather_primary, null))

                tvLife.setTextColor(resources.getColor(R.color.default_gray, null))
                imageLife.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvProfile.setTextColor(resources.getColor(R.color.default_gray, null))
                imageProfile.setColorFilter(resources.getColor(R.color.default_gray, null))
            }
            1 -> {
                tvHome.setTextColor(resources.getColor(R.color.default_gray, null))
                imageHome.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvLife.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageLife.setColorFilter(resources.getColor(R.color.weather_primary, null))

                tvProfile.setTextColor(resources.getColor(R.color.default_gray, null))
                imageProfile.setColorFilter(resources.getColor(R.color.default_gray, null))
            }
            2 -> {
                tvHome.setTextColor(resources.getColor(R.color.default_gray, null))
                imageHome.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvLife.setTextColor(resources.getColor(R.color.default_gray, null))
                imageLife.setColorFilter(resources.getColor(R.color.default_gray, null))

                tvProfile.setTextColor(resources.getColor(R.color.weather_primary, null))
                imageProfile.setColorFilter(resources.getColor(R.color.weather_primary, null))
            }
        }
    }
}